﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.DirectoryServices.AccountManagement;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.Support;

namespace MS.InformationManagement.BLL.ValidarPassword
{
    public class ValidarPassword
    {
        public static bool ValidarUsuarioLocal(Guid GuidCurrentList, string PassWord, int ItemID, string form)
        {
            bool result = false;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                if (form == "Prestamo")
                    UpdatePrestamo(ItemID);

                if (form == "Devolucion")
                    AceptarDevolucion(ItemID);

                result = true;
            });

            return result;
        }

        public static bool ValidarUsuarioActiveDirectory(Guid GuidCurrentList, string PassWord, int ItemID, string form)
        {
            bool result = false;
            try 
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite currentSite = new SPSite(SPContext.Current.Web.Url))
                    {
                        using (SPWeb currentWeb = currentSite.OpenWeb())
                        {
                            SPList currentList = currentWeb.Lists[GuidCurrentList];
                            SPUser currentUser = currentWeb.EnsureUser(currentList.Title);
                            string Domain = currentUser.LoginName.Split('\\')[0];
                            string UserName = currentUser.LoginName.Split('\\')[1];
                            try
                            {
                                ContextType ctx = ContextType.Domain;
                                using (PrincipalContext pc = new PrincipalContext(ctx, Domain))
                                {

                                    result = pc.ValidateCredentials(UserName, PassWord);
                                    if (result)
                                    {
                                        if (form == "Prestamo")
                                            UpdatePrestamo(ItemID);

                                        if (form == "Devolucion")
                                            AceptarDevolucion(ItemID);
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                throw new Exception("No se encuentra el contexto del direcorio activo");
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return result;
        }

        private static bool UpdatePrestamo(int ItemID)
        {
            bool result = false;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPUserToken systoken = SPContext.Current.Site.SystemAccount.UserToken;
                using (SPSite currentSite = new SPSite(SPContext.Current.Web.Url, systoken))
                {
                    using (SPWeb currentWeb = currentSite.OpenWeb())
                    {
                        currentWeb.AllowUnsafeUpdates = true;
                        SPList listPrestamos = currentWeb.Lists.TryGetList(GlobalConstants.LIST_PRESTAMOS);
                        SPListItem newItem = listPrestamos.Items.GetItemById(ItemID);
                        newItem[CustomSiteColumns.FIELD_SOLICITUD_PRESTAMOS_ESTADO] = EstadoDePrestamo.Aceptado;
                        newItem[CustomSiteColumns.FIELD_SOLICITUD_PRESTAMOS_FECHA_PRESTAMO] = DateTime.Now;
                        newItem.Update();
                        currentWeb.AllowUnsafeUpdates = false;
                        result = true;
                    }
                }
            });


            return result;
        }

        public static bool AceptarDevolucion(int ItemID)
        {
            bool result = false;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                SPUserToken systoken = SPContext.Current.Site.SystemAccount.UserToken;
                using (SPSite currentSite = new SPSite(SPContext.Current.Web.Url, systoken))
                {
                    using (SPWeb currentWeb = currentSite.OpenWeb())
                    {
                        currentWeb.AllowUnsafeUpdates = true;
                        SPList prestamosList = currentWeb.Lists.TryGetList(GlobalConstants.LIST_PRESTAMOS);
                        SPListItem item = prestamosList.GetItemById(ItemID);
                        item[CustomSiteColumns.FIELD_SOLICITUD_PRESTAMOS_FECHA_DEVOLCION] = DateTime.Now;
                        item[CustomSiteColumns.FIELD_SOLICITUD_PRESTAMOS_ESTADO] = EstadoDePrestamo.Devuelto;
                        item.Update();
                        currentWeb.AllowUnsafeUpdates = false;
                        result = true;
                    }
                }
            });
            return result;
        }
    }
}
